# -*- coding:utf-8 -*-
from tkinter.messagebox import *
from tkinter import *
from tkinter import ttk
from student_info_sql import *
from teacher_info_sql import *
from student_achievement_sql import *
from AdminPage import *
from StudentPage import *
from TeacherPage import *

import xlwt

global numbers, i
i = 0


class LoginPage(object):
    def __init__(self, master=None):
        self.root = master
        winWidth = 650
        winHeight = 450
        screenWidth = self.root.winfo_screenwidth()
        screenHeight = self.root.winfo_screenheight()

        x = int((screenWidth - winWidth) / 2)
        y = int((screenHeight - winHeight) / 2)
        # 设置窗口初始位置在屏幕居中
        self.root.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
        # 设置窗口图标
        # root.iconbitmap("./image/icon.ico")
        # 设置窗口宽高固定
        self.root.resizable(0, 0)
        self.student_number = StringVar()
        self.student_pw = StringVar()
        self.createPage()

    def getNumbers(self):
        global numbers
        return numbers

    def createPage(self):
        '''
        登录页面
        1:创建图片组件
        2:根目录基础上添加Frame容器
        3:Frame容器上添加注册控件
        '''
        bm = PhotoImage(file=r'cxk.gif')
        self.lab3 = Label(self.root, image=bm)
        self.lab3.bm = bm
        self.lab3.pack()

        self.page = Frame(self.root)
        self.page.pack()
        Label(self.page).grid(row=0, stick=W)
        Label(self.page, text='学号: ').grid(row=1, stick=W, pady=10)
        Entry(self.page, textvariable=self.student_number).grid(row=1, column=1, stick=E)
        Label(self.page, text='密码: ').grid(row=2, stick=W, pady=10)
        Entry(self.page, textvariable=self.student_pw, show='*').grid(row=2, column=1, stick=E)
        Button(self.page, text='管理员登录', command=self.admin_loginCheck).grid(row=3, column=0)
        # self.root.bind('<KeyPress-Return>',self.admin_loginCheck1)#绑定键盘上的回车登录
        Button(self.page, text='学生注册', command=self.signup).grid(row=3, column=3)
        Button(self.page, text='学生/教师登录', command=self.student_loginCheck).grid(row=3, column=1)
        # self.root.bind('<KeyPress-Return>',self.user_loginCheck1)#绑定键盘上的回车登录

    def admin_loginCheck(self):
        global numbers
        '''
        管理员登录
        1:获取管理员账号与密码
        2:将获取到的账号与密码与数据库文件配对，配对成功返回值为正确，否则为错误
        3:将返回值判断，正确则登录界面清除，登录界面图片清除，进入管理员界面
        异常捕获：未填写账号或者密码
        '''
        try:
            Admin_number = self.student_number.get()
            # print(User_id)
            Admin_pw = self.student_pw.get()
            # print(User_pw)
            #             pd=admin_Select_id_pw(Admin_id,Admin_pw)
            #             if pd:
            if Admin_number == "1" and Admin_pw == "1":
                self.page.destroy()
                self.lab3.pack_forget()
                AdminPage(self.root)
            else:
                showinfo(title='错误', message='账号或密码错误！')
        except:
            showinfo(title='错误', message='输入错误，请重新输入！')

    def student_loginCheck(self):
        global numbers, i
        '''
        学生登录
        1:获取学生学号与密码
        2:将获取到的学号与密码与数据库文件配对，配对成功返回值为正确，否则为错误
        3:将返回值判断，正确则登录界面清除，登录界面图片清除，进入用户界面，异常捕获：未填写账号或者密码
        '''
        try:
            Student_number = self.student_number.get()
            # print(User_id)
            Student_pw = self.student_pw.get()
            # print(User_pw)
            pd_student = user_slect_number_pw(Student_number, Student_pw)
            pd_teacher = teacher_slect_number_pw(Student_number, Student_pw)
            if pd_student != None:
                numbers = Student_number
                self.page.destroy()
                self.lab3.pack_forget()
                StudentPage(numbers, self.root)
            elif pd_teacher != None:
                numbers = Student_number
                self.page.destroy()
                self.lab3.pack_forget()
                TeacherPage(numbers, self.root)

            elif i > 2:
                showinfo(title='错误', message='密码三次输入错误，此次登录被终止！')
                self.root.destroy()
            else:
                i += 1
                showinfo(title='错误', message='账号或密码错误！')
        except Exception as e:
            print(e)
            print('文件', e.__traceback__.tb_frame.f_globals['__file__'])
            print('行号', e.__traceback__.tb_lineno)
            showinfo(title='错误', message='输入错误，请重新输入！')

    def signup(self):
        '''
        学生注册页面
        1:新建一个置于顶层的窗口
        2:将布局控件放入
        3:每个窗口的控件布局必须是一致的，place(),grid(),pack()中的一种
        '''

        def insert_sql():
            '''
            添加学生
            1:获取学生姓名，年龄，学号，密码
            2:将获取到的账号与数据库文件配对，查看是否存在相同学号，如不存在，将学生插入数据库文件，存在则提示修改账户名
            异常捕获：信息未填写
            '''
            try:
                age = self.new_age.get()
                number = self.new_number.get()
                name = self.new_name.get()
                pw = self.new_pw.get()
                if len(number) < 12:
                    showinfo(title='提示', message='学号为12位的数字，请重新输入！')
                else:
                    XWC = user_showdb(number)  # 先判断账号是否存在于学生或者教师数据库
                    SHB = teacher_showdb(number)
                    if XWC == None and SHB == None:
                        user_insertData(number, name, pw, age)
                        showinfo(title='提示', message='注册成功')
                        self.window_sign_up.destroy()
                    else:
                        self.window_sign_up.destroy()
                        showinfo(title='提示', message='学号重复，注册失败，请修改学号！')
            except:
                self.window_sign_up.destroy()
                showinfo(title='错误', message='未知错误，请重新输入！')

        self.window_sign_up = Toplevel(self.root)
        winWidth = 300
        winHeight = 200
        self.window_sign_up.title('注册窗口')
        screenWidth = self.window_sign_up.winfo_screenwidth()
        screenHeight = self.window_sign_up.winfo_screenheight()
        x = int((screenWidth - winWidth) / 2)
        y = int((screenHeight - winHeight) / 2)
        # 设置窗口初始位置在屏幕居中
        self.window_sign_up.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x - 50, y - 50))
        # 设置窗口图标
        # root.iconbitmap("./image/icon.ico")
        # 设置窗口宽高固定
        self.window_sign_up.resizable(0, 0)

        self.new_name = StringVar()
        Label(self.window_sign_up, text='姓名: ').place(x=10, y=10)
        entry_new_name = Entry(self.window_sign_up, textvariable=self.new_name)
        entry_new_name.place(x=130, y=10)

        self.new_age = StringVar()
        Label(self.window_sign_up, text='年龄: ').place(x=10, y=50)
        entry_usr_age = Entry(self.window_sign_up, textvariable=self.new_age)
        entry_usr_age.place(x=130, y=50)

        self.new_number = StringVar()
        Label(self.window_sign_up, text='学号: ').place(x=10, y=90)
        entry_student_number = Entry(self.window_sign_up, textvariable=self.new_number)
        entry_student_number.place(x=130, y=90)

        self.new_pw = StringVar()
        Label(self.window_sign_up, text='密码: ').place(x=10, y=130)
        entry_usr_pw = Entry(self.window_sign_up, textvariable=self.new_pw, show='*')
        entry_usr_pw.place(x=130, y=130)

        sign_up = Button(self.window_sign_up, text='注册', command=insert_sql)
        sign_up.place(x=237, y=160)
